# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openimageio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
#pkgver=1.5.24
pkgver=1.6.15
pkgrel=1
pkgdesc="A library for reading and writing images, including classes, utilities, and applications (mingw-w64)"
arch=('any')
url="http://www.openimageio.org/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-field3d"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-boost")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-qt4: iv image viewer"
            "${MINGW_PACKAGE_PREFIX}-python2: bindings support")
options=('strip' 'buildflags')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageIO/oiio/archive/Release-${pkgver}.tar.gz
        fix-mingw-w64.patch
        workaround-ansidecl-h-PTR-define-conflict.patch
        0001-MinGW-w64-include-winbase-h-early-for-TCHAR-types.patch)
sha256sums=('07e7d30a2f678d519af17adeb13352a4b80fb380f4458255e1a0152000ce8c90'
            'fd3b3ae804e89e164fd9ca07373452643b66cb4dee49306dd3a4e3b5f9249995'
            'ab6bde02c0c05495a9cb13441535d42f4d5929bd930859d75f589d12be9a182b'
            'b7cc78d0f535ceb1aba68b11970c32ffbf04815d229b12e64e2c2c0b3403b4b2')

prepare() {
  cd ${srcdir}/oiio-Release-${pkgver}
  patch -p1 -i ${srcdir}/fix-mingw-w64.patch
  patch -p1 -i ${srcdir}/workaround-ansidecl-h-PTR-define-conflict.patch
  patch -p1 -i ${srcdir}/0001-MinGW-w64-include-winbase-h-early-for-TCHAR-types.patch
}

build() {
  export OPENJPEG_HOME=${MINGW_PREFIX}
  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYLIB_INSTALL_DIR=lib/python2.7/site-packages \
    -DUSE_FREETYPE=ON \
    -DFREETYPE_INCLUDE_PATH=${MINGW_PREFIX}/include/freetype2 \
    -DOPENJPEG_INCLUDE_DIR=${MINGW_PREFIX}/include/openjpeg-1.5 \
    -DOPENJPEG_OPENJPEG_LIBRARIES=${MINGW_PREFIX}/lib/libopenjpeg.dll.a \
    -DUSE_OPENCV=ON \
    -DUSE_FIELD3D=ON \
    -DUSE_OPENSSL=ON \
    -DCMAKE_SHARED_LINKER_FLAGS=" -Wl,--export-all-symbols -Wl,--enable-auto-import " \
    -DOIIO_BUILD_TESTS=ON \
    ../oiio-Release-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install
  rm -r ${pkgdir}${MINGW_PREFIX}/doc

  install -Dm644 ${srcdir}/oiio-Release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
